package com.hbnu.jdbc;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author kittychen
 * @since 2025-10-24 8:07
 */
public class JdbcDemo04 {

    public static void main(String[] args) {
        Map<String, String> userInfo = initUI();

        boolean flag = verifiedUserInfo(userInfo);

        System.out.println(flag ? "登录成功" : "账号或密码错误");
    }

    private static boolean verifiedUserInfo(Map<String, String> userInfo) {
        boolean flag = false;   // 登录成功或失败的标记

        // 从用户信息userInfo中获取账号密码
        String username = userInfo.get("username");
        String password = userInfo.get("password");

        Connection connection = null;
        // Statement statement = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            String url = "jdbc:mysql://127.0.0.1:3306/class2311?serverTimezone=GMT&useSSL=false&characterEncoding=utf8";
            String name = "root";
            String pwd = "123456";
            connection = DriverManager.getConnection(url, name, pwd);

            // statement = connection.createStatement();
            // sql: select * from tb_user where username = 'username' and password = 'password'
            // String sql = "select * from tb_user where username = '" + username + "' and password = '" + password + "'";
            // resultSet = statement.executeQuery(sql);

            String sql = "select * from tb_user where username = ? and password = ?";   // ? 是占位符，参与编译
            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                flag = true;
                break;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

        return flag;
    }

    private static Map<String, String> initUI() {
        Map<String, String> userInfo = new HashMap<>();   //  用于存放用户的账号密码信息

        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入你的账号：");
        String username = scanner.nextLine();
        System.out.println("请输入你的密码：");
        String password = scanner.nextLine();

        // 将用户输入的账号密码存入集合
        userInfo.put("username", username);
        userInfo.put("password", password);

        return userInfo;
    }
}
